import constant from "../../utils/constant"
import services from "../../services/index"
import { UserInfoVO } from "../../services/baseClass"
import { wxLogin } from "../../utils/util"

Page({

  data: {
     //role: 2,// 测试门卫
    // role: 6,// 测试老师
     role: 10,// 测试管理员
  },
  onEnterReport() {
    wx.navigateTo({
      url: '../report/enter/enterReport'
    })
  },
  onOuterReport() {
    wx.navigateTo({
      url: '../report/out/outerReport'
    })
  },
  onAuditReport() {
    wx.navigateTo({
      url: '../audit/list/auditList'
    })
  },
  onMyReport() {
    wx.navigateTo({
      url: '../report/list/myReport'
    })
  },
  onScanValid() {
    wx.scanCode({
      success(qrCode) {
        //TODO get orderInfo by qrID
        const qr_code = qrCode.result.split('&&')[0];
        wx.setStorageSync("index/index", qr_code)
        wx.navigateTo({ url: "../verfy/index" })
      }
    })

  },
  
  onLoad:async function () {
    // TODO 报备历史获取 by userinfo
    // 会设置this.data.role = 2 | 6 | 10   =>   门卫 | 老师 | 管理
    let roles = wx.getStorageSync(constant.ROLE_LIST);
    //roles = null;
    if (!roles) {
      wxLogin();
      let res = await services.API.user.getCurrentUser.request();
      let user;
      if(res&&res.data){
        user = res.data;     
        console.log("aaaaa",roles);  
        wx.setStorageSync(constant.USER_INFO, user);
        roles = user?.roles;
        wx.setStorageSync(constant.ROLE_LIST,roles);
      }
     }
     console.log("sssss",roles);
     this.onLoadGetUserRole(roles)
  },

 
  onLoadGetUserRole(roles: string[]) {
    if (!roles) return;
    if (roles.some(role => role == 'Admin')) {
      this.setData({ role: 10 })
      return;
    }
    if (roles.some(role => role == 'Teacher')) {
      this.setData({ role: 6 })
      return;
    }
    if (roles.some(role => role == 'Doorman')) {
      this.setData({ role: 2 })
      return;
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})